﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Threading.Tasks;
using YiSha.Util;
using YiSha.Util.Extension;
using YiSha.Util.Model;
using YiSha.Entity.QixingManage;
using YiSha.Model.Param.QixingManage;
using YiSha.Service.QixingManage;

namespace YiSha.Business.QixingManage
{
    /// <summary>
    /// 创 建：admin
    /// 日 期：2024-11-05 22:04
    /// 描 述：业务类
    /// </summary>
    public class GoodsTypesBLL
    {
        private GoodsTypesService goodsTypesService = new GoodsTypesService();

        #region 获取数据
        public async Task<TData<List<GoodsTypesEntity>>> GetList(GoodsTypesListParam param)
        {
            TData<List<GoodsTypesEntity>> obj = new TData<List<GoodsTypesEntity>>();
            obj.Data = await goodsTypesService.GetList(param);
            obj.Total = obj.Data.Count;
            obj.Tag = 1;
            return obj;
        }

        public async Task<TData<List<GoodsTypesEntity>>> GetPageList(GoodsTypesListParam param, Pagination pagination)
        {
            TData<List<GoodsTypesEntity>> obj = new TData<List<GoodsTypesEntity>>();
            obj.Data = await goodsTypesService.GetPageList(param, pagination);
            obj.Total = pagination.TotalCount;
            obj.Tag = 1;
            return obj;
        }

        public async Task<TData<List<GoodsTypeBrand>>> GetGoodsBrandModel(GoodsTypesParam param)
        {
            var db = goodsTypesService.BaseRepository();

            var brands=db.IQueryable<ShopBrandRelation>(t => t.shopid == param.shopid).Select(t => t.brandid).ToList();

            var data = db.FindList<GoodsTypeBrand>(t => t.typeid == param.id && brands.Contains(t.brandid)).Result.ToList();
            TData<List<GoodsTypeBrand>> obj = new TData<List<GoodsTypeBrand>>();
            obj.Data = data;
            obj.Tag = 1;
            obj.Message = "成功";
            return obj;
        }

        public async Task<TData<GoodsTypesEntity>> GetEntity(long id)
        {
            TData<GoodsTypesEntity> obj = new TData<GoodsTypesEntity>();
            obj.Data = await goodsTypesService.GetEntity(id);
            if (obj.Data != null)
            {
                obj.Tag = 1;
            }
            return obj;
        }

        //public async Task<TData<List<BaseGoodsAttrDto>>> GetBaseGoodsAttr(long id) {

        //    var db = goodsTypesService.BaseRepository();

        //    var data = db.FindList<GoodsAttributesEntity>(t => t.goods_types_id == id && !t.attribute_name.Equals("已用年限") && !t.attribute_name.Equals("成色")).Result.Select(t => new BaseGoodsAttrDto() { Id = t.Id.Value, name = t.attribute_name}).ToList();

        //    foreach (var item in data)
        //    {
        //        item.list = db.FindList<GoodsExtendedAttributesEntity>(t => t.goods_attributes_id == item.Id).Result.Select(t=>new GoodsExtendedAttributesDto() { 
        //            extended_attribute_name=t.extended_attribute_name,
        //        }).ToList();
        //    }


        //    TData<List<BaseGoodsAttrDto>> obj = new TData<List<BaseGoodsAttrDto>>();
        //    obj.Data = data.Select(t => new BaseGoodsAttrDto() { name = t.name, list = t.list }).ToList();
        //    obj.Tag = 1;
        //    obj.Message = "成功";

        //    return obj;
        //}

        public async Task<TData<List<BrandModelZtree>>> GetBrandModel()
        {
            var db = goodsTypesService.BaseRepository();
            var data = db.FindList<BrandInfoEntity>().Result.Select(t => new BrandModelZtree()
            {
                value = t.Id.ToString(),
                label=t.brand_name
            }).ToList();

            foreach (var item in data)
            {
                var brandid=long.Parse(item.value);
                var datachildren = db.FindList<ModelsEntity>(t => t.brandid == brandid).Result.Select(t => new BrandModelZtree()
                {
                    value = t.Id.ToString(),
                    label = t.model_name,
                    children = null
                }).ToList();
                if (datachildren.Count > 0)
                {
                    item.children = datachildren;
                }
                else
                {
                    item.children = null;
                }
            }

            TData<List<BrandModelZtree>> obj = new TData<List<BrandModelZtree>>();
            obj.Data = data;
            obj.Tag = 1;
            obj.Message = "成功";

            return obj;
        }

        public async Task<TData<List<GoodsAttributesEntity>>> GetBaseGoodsAttr(long id)
        {

            var db = goodsTypesService.BaseRepository();

            var data = db.FindList<GoodsAttributesEntity>(t=>t.goods_types_id==id).Result.OrderByDescending(t=>t.sort).ToList();

            var maxSortData = data.OrderByDescending(d => d.sort).FirstOrDefault();

            if (maxSortData != null)
            {
                // 将 AllowFile 设置为 true
                maxSortData.allowfile = true;
            }
            TData<List<GoodsAttributesEntity>> obj = new TData<List<GoodsAttributesEntity>>();
            obj.Data = data;
            obj.Tag = 1;
            obj.Message = "成功";

            return obj;
        }

        #endregion

        #region 提交数据
        public async Task<TData<string>> SaveForm(GoodsTypesEntity entity)
        {
            TData<string> obj = new TData<string>();
            await goodsTypesService.SaveForm(entity);
            obj.Data = entity.Id.ParseToString();
            obj.Tag = 1;
            return obj;
        }

        public async Task<TData> DeleteForm(string ids)
        {
            TData obj = new TData();
            await goodsTypesService.DeleteForm(ids);
            obj.Tag = 1;
            return obj;
        }
        #endregion

        #region 私有方法
        #endregion
    }
}
